package edu.hawaii.myisern.commandline;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;

import org.junit.Test;

/**
 * Demonstrates that the sample code is working. 
 * 
 * @author Philip Johnson
 * @author Phuoc Le
 * @author Ka Yee Leung
 * @author Jared Sunouchi
 */
public class TestMyIsernXmlLoader {
  
  // files used for testing
  private String userDirectory = "user.dir";
  private File validCollaborationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.collaborations.example.xml");
  private File validOrganizationFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.organizations.example.xml");
  private File validResearcherFile = new File(System.getProperty(userDirectory)
      + "/xml/tests/test.researchers.example.xml");

  /**
   * Tests the methods of MyIsernXmlLoader. 
   * @throws Exception If problems occur.
   */
  @Test public void testLoader() throws Exception {
    MyIsernXmlLoader loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        validResearcherFile);
    
    // verifies the methods to obtain size of collaborations, organizations or researchers
    assertEquals("Check collaborations size", 1, loader.getNumCollaborations());
    assertEquals("Check organizations size", 2, loader.getNumOrganizations());
    assertEquals("Check researchers size", 3, loader.getNumResearchers());
    
    // verifies getCollaborationsXml() returns the correct type
    assertTrue("Check Collaborations", loader.getCollaborationsXml().contains("<Collaborations>"));
    // verifies getOrganizationsXml() returns the correct type
    assertTrue("Check Organizations", loader.getOrganizationsXml().contains("<Organizations>"));
    // verifies getResearchersXml() returns the correct type
    assertTrue("Check Researchers", loader.getResearchersXml().contains("<Researchers>"));
    
    // verifies getXmlFilesNameAndDirectory() is returning the right type
    assertTrue("Check File Names and Directory", 
        loader.getXmlFilesName().contains("collaborations"));
    assertTrue("Check File Names and Directory", 
        loader.getXmlFilesName().contains("organizations"));
    assertTrue("Check File Names and Directory", 
        loader.getXmlFilesName().contains("researchers"));
    
    // makes sure the correct ID is generated
    assertEquals("Check if unique ID can be generated", "testing_id", 
        loader.createUniqueId("testing id"));
    assertEquals("Check if unique ID is not generated", null, loader.createUniqueId("testing id"));
    
    // verifies checkValidLink produces correct results
    assertEquals("Check if link is valid", true, 
        loader.checkValidLink("http://www.google.com", "unique_ID"));
    
    // validate data in the 3 given XML files.
    loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        validResearcherFile);
    assertEquals("Check that collaborations, organizations, and researchers XMLs have valid data",
        true, loader.sanityCheck());
    
    
    File collaborationsFile;
    File organizationsFile;
    File researchersFile;
    
    // validates checkXmlDataAndGenerateUniqueId()
    collaborationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test1.collaborations.example.xml");
    loader = new MyIsernXmlLoader(collaborationsFile, validOrganizationFile, 
        validResearcherFile);
    assertEquals("Check collaboration name for null", false, 
        loader.sanityCheck());
    
    organizationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test1.organizations.example.xml");
    loader = new MyIsernXmlLoader(validCollaborationFile, organizationsFile, 
        validResearcherFile);
    assertEquals("Check organization name for null", false, 
        loader.sanityCheck());
    
    researchersFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test1.researchers.example.xml");
    loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        researchersFile);
    assertEquals("Check researcher name for null", false, 
        loader.sanityCheck());
    
    collaborationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test3a.collaborations.example.xml");
    loader = new MyIsernXmlLoader(collaborationsFile, validOrganizationFile, 
        validResearcherFile);
    assertEquals("Check for valid date", false, 
        loader.sanityCheck());
    
    collaborationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test3b.collaborations.example.xml");
    loader = new MyIsernXmlLoader(collaborationsFile, validOrganizationFile, 
        validResearcherFile);
    assertEquals("Check for valid date", false, 
        loader.sanityCheck());
    
    organizationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test4a.organizations.example.xml");
    loader = new MyIsernXmlLoader(validCollaborationFile, organizationsFile, 
        validResearcherFile);
    assertEquals("Check for unique id generation for organizations", false, 
        loader.sanityCheck());
    
    researchersFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test4a.researchers.example.xml");
    loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        researchersFile);
    assertEquals("Check for unique id generation for researchers", false, 
        loader.sanityCheck());
    
    collaborationsFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test4a.collaborations.example.xml");
    loader = new MyIsernXmlLoader(collaborationsFile, validOrganizationFile, 
        validResearcherFile);
    assertEquals("Check for unique id generation for collaborations", false, 
        loader.sanityCheck());
    
    researchersFile = new File(System.getProperty(userDirectory)
        + "/xml/tests/test4b.researchers.example.xml");
    loader = new MyIsernXmlLoader(validCollaborationFile, validOrganizationFile, 
        researchersFile);
    assertEquals("Check for unique id generation between researchers and organizations", false, 
        loader.sanityCheck());
  }
}
